package com.gz.moreaseManager.sys.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.gz.moreaseManager.sys.entity.Role;
import com.gz.moreaseManager.sys.entity.UserRole;
import com.gz.moreaseManager.sys.mapper.UserRoleMapper;
import com.gz.moreaseManager.sys.service.IUserRoleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gz.moreaseManager.utils.UtilTool;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户权限表 服务实现类
 * </p>
 *
 * @author wike
 * @since 2019-02-19
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {

    @Resource
    private UserRoleMapper userRoleMapper;


    @Override
    public List<Role> findRoleListByUserId(Integer userId) {
        return userRoleMapper.findRoleListByUserId(userId);
    }

    @Override
    @Transactional
    public void insertBatchByUserId( Integer userId, Integer[] roleIds){

        if(UtilTool.isNotBlank( roleIds ) && UtilTool.isNotBlank( userId )){
            //先删除原有用户角色
            Wrapper wrapper = new EntityWrapper<UserRole>();
            wrapper.eq("user_id",userId);
            userRoleMapper.delete(wrapper);

            for(Integer roleId :roleIds){
                UserRole userRole = new UserRole();
                userRole.setRoleId(roleId);
                userRole.setUserId(userId);
                userRole.setCreateTime(new Date());

                userRoleMapper.insert( userRole );
            }
        }
    }
}
